import os, ctypes, time, signal
from hi_video_lib import VideoProxy

lib_common = ctypes.cdll.LoadLibrary("../libs/libHiRTPCommon.so")


def sigint_handler(signum, frame):
    VideoProxy.release()

    print('catched interrupt signal!')
    time.sleep(1)
    exit(-1)

signal.signal(signal.SIGINT, sigint_handler)
signal.signal(signal.SIGHUP, sigint_handler)
signal.signal(signal.SIGTERM, sigint_handler)

save_index = 0
def __on_msg(video_id, img):
    global save_index
    try:
        char_p_test = bytes("./bmp/" + str(video_id) + "/" + str(save_index % 100) + '.bmp',"utf8")
        lib_common.hi_saveBmp(char_p_test, img.tobytes(), 1920, 1080)
        save_index = save_index + 1
        print('save pic success:', char_p_test)    
    except Exception as e:
        print('save pic exception:' + str(e))


save_index2 = 0
def __on_msg2(video_id, img):
    global save_index2
    try:
        char_p_test = bytes("./bmp/" + str(video_id) + "/" + str(save_index2 % 100) + '.bmp',"utf8")
        lib_common.hi_saveBmp(char_p_test, img.tobytes(), 1920, 1080)
        save_index2 = save_index2 + 1
        print('save pic success:', char_p_test)    
    except Exception as e:
        print('save pic exception:' + str(e))



video = VideoProxy()
video.start("rtmp://58.200.131.2:1935/livetv/gxtv", 60002, 1920, 1080, 300001, __on_msg)

video2 = VideoProxy()
video2.start("rtmp://58.200.131.2:1935/livetv/gxtv", 60003, 1920, 1080, 300002, __on_msg2)
time.sleep(10000)